import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../views/Layout'
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error)
}
Vue.use(VueRouter)

const routes = [{
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/')
  },
  {
    path: '/',
    component: Layout,
    children: [{
        path: '/',
        component: () => import('@/views/StoreManagement'),
      },
      {
        path: '/commodity',
        component: () => import('@/views/Commodity/Goodsadd'),
      },
      {
        path: '/commodity/goodsadd',
        name: 'commodity/goodsadd',
        component: () => import('@/views/Commodity/Goodsadd'),
      },
      {
        path: '/commodity/goodslist',
        name: 'goodslist',
        component: () => import('@/views/Commodity/Goodslist'),
      },
      {
        path: '/commodity/goodsedit/',
        name: 'goodsedit',
        component: () => import('@/views/Commodity/Goodslist/goodsEdit.vue'),
      },
      {
        path: '/commodity/goodslistoff/',
        name: 'goodslistoff',
        component: () => import('@/views/Commodity/Goodslist/goodslistoff.vue'),
      },
      {
        path: '/commodity/recommend/',
        name: 'recommend',
        component: () => import('@/views/Commodity/Goodslist/recommend.vue'),
      },
      {
        path: '/order',
        name: 'orderlist',
        component: () => import('@/views/orderManage/orderlist'),
      },
      {
        path: '/cancel/order',
        name: 'cancelorder',
        component: () => import('@/views/orderManage/cancelOrder'),
      },
      {
        path: '/feedback',
        name: 'feedback',
        component: () => import('@/views/Feedback'),
      },
      {
        path: '/store',
        component: () => import('@/views/StoreManagement'),
      },
      {
        path: '/store/management',
        name: 'storemanagement',
        component: () => import('@/views/StoreManagement'),
      },
      {
        path: '/store/address',
        name: 'storeaddress',
        component: () => import('@/views/StoreManagement/afterAddress.vue'),
      },
      {
        path: '/store/freight',
        name: 'freightemplate',
        component: () => import('@/views/StoreManagement/freightTemplate.vue'),
      },
      {
        path: '/store/slideshow',
        name: 'slideshow',
        component: () => import('@/views/StoreManagement/slideshow.vue'),
      },
      {
        path: '/tim',
        name: 'tim',
        component: () => import('@/views/TXtim'),
      },
      {
        path: '/traceability',
        name: 'Traceability',
        component: () => import('@/views/Traceability'),
      },
      {
        path: '/traceability/brand',
        name: 'brand',
        component: () => import('@/views/Traceability/manage/brand.vue'),
      },
      {
        path: '/traceability/configu',
        name: 'configu',
        component: () => import('@/views/Traceability/manage/configu.vue'),
      },
      {
        path: '/traceability/marketing',
        name: 'marketing',
        component: () => import('@/views/Traceability/manage/marketing.vue'),
      },
      {
        path: '/traceability/roler',
        name: 'roler',
        component: () => import('@/views/Traceability/manage/roler.vue'),
      },
      {
        path: '/traceability/statistical',
        name: 'statistical',
        component: () => import('@/views/Traceability/manage/statistical.vue'),
      },
      {
        path: '/traceability/traceab',
        name: 'traceab',
        component: () => import('@/views/Traceability/manage/traceab.vue'),
      },
      {
        path: '/traceability/applet',
        name: 'applet',
        component: () => import('@/views/Traceability/manage/applet.vue'),
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 拦截
router.beforeEach((to, from, next) => {
  let token = window.localStorage.getItem('intrachaincode')
  if (token) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
